﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ESoft.Core.Data.DAO;
using ESoft.Core.Data.Entity;
using ESoft.Core.Interceptor;
using ESoft.Core.Web.VO;

namespace ESoft.Core.Data.Service.Impl
{
  
    public class BaseService<T, TKey> : IBaseService<T, TKey> where T : BaseModel<TKey>, new()
    {
        protected readonly IBaseDAO<T, TKey> _baseDao;

        public BaseService(IBaseDAO<T, TKey> dao)
        {
            _baseDao = dao;
        }

        [Transactional]
        public T Add(T entity)
        {
            return _baseDao.Add(entity);
        }

        public int Count(Expression<Func<T, bool>> predicate)
        {
            return _baseDao.Count(predicate);
        }

        public bool Delete(IEnumerable<T> entities)
        {
            return _baseDao.Delete(entities);
        }

        public bool Delete(T entity)
        {
            return _baseDao.Delete(entity);
        }

        public bool Delete(TKey id)
        {
            return _baseDao.Delete(id);
        }

        public bool Exist(Expression<Func<T, bool>> predicate)
        {
            return _baseDao.Exist(predicate);
        }

        public T Find(TKey id)
        {
            return _baseDao.Find(id);
        }

        public T First(Expression<Func<T, bool>> predicate)
        {
            return _baseDao.First(predicate);
        }

        public IQueryable<T> LoadEntities()
        {
            return _baseDao.LoadEntities();
        }

        public IQueryable<T> LoadEntities(Expression<Func<T, bool>> predicate)
        {
            return _baseDao.LoadEntities(predicate);
        }

        public IQueryable<T> LoadPageEntities<TS>(PageInput page, Expression<Func<T, bool>> predicate, Expression<Func<T, TS>> keySelector, bool isAsc)
        {
            return _baseDao.LoadPageEntities(page, predicate, keySelector, isAsc);
        }

        public decimal Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, decimal>> selector)
        {
            return _baseDao.Sum(predicate, selector);
        }

        public int Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector)
        {
            return _baseDao.Sum(predicate, selector);
        }

        public bool Update(T entity)
        {
            return _baseDao.Update(entity);
        }
    }
}
